package com.li.controller;

import com.li.common.ModelName;
import com.li.config.AiConfig;
import com.li.dao.RedisChatMemoryStore;
import com.li.utils.IdHandle;
import com.li.utils.IdUtils;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.TokenStream;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.Objects;

@RestController
@RequestMapping("/ai/dao")
public class ChatDaoController {
    @Resource
    QwenChatModel qwenChatModel;
    @Resource
    QwenStreamingChatModel qwenStreamingChatModel;
    @Resource
    RedisTemplate<String,String> redisTemplate;
    @Resource
    IdUtils idUtils;
    @Resource
    IdHandle idHandle;

    @RequestMapping("/chat/chatMemory/{id}")
    public String memoryChat(@PathVariable(name = "id") Integer id, @RequestParam(name = "message") String message)
    {
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .id(id)
                .maxMessages(10)
                .chatMemoryStore(new RedisChatMemoryStore(redisTemplate,"memoryId", 60 * 60 * 24 *5))
                .build();
        AiConfig.Assistant assistant = AiServices.builder(AiConfig.Assistant.class)
                .chatModel(qwenChatModel)
                .streamingChatModel(qwenStreamingChatModel)
                .chatMemoryProvider(uid -> chatMemory)
                .build();

        //简单处理 :如果uid 为null ==> throw error;如果uid 搜不到 ,直接创建一个uid  ;如果uid 搜到 ,则使用uid
        String uid = idHandle.uidParse(id, ModelName.QWQ_32B_PREVIEW);
        return assistant.chat(uid,message);
    }
    @RequestMapping(value = "/stream/chatMemoryByStream/{id}",produces ="text/stream;charset=UTF-8")
    public Flux<String> memoryChatByStream(@PathVariable(name = "id") Integer id, @RequestParam(name = "message") String message)
    {

        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .id(id)
                .maxMessages(10)
                .chatMemoryStore(new RedisChatMemoryStore(redisTemplate,"memoryId", 60 * 60 * 24 *5))
                .build();
        AiConfig.Assistant assistant = AiServices.builder(AiConfig.Assistant.class)
                .chatModel(qwenChatModel)
                .streamingChatModel(qwenStreamingChatModel)
                .chatMemoryProvider(uid -> chatMemory)
                .build();

        String uid = idHandle.uidParse(id, ModelName.QWQ_PLUS);
        TokenStream tokenStream = assistant.streamChat(uid, message);
        return Flux.create(sink -> {
            tokenStream.onPartialResponse(sink::next)
                    .onCompleteResponse(response -> {
                        System.out.println(response);
                        sink.complete();
                    })
                    .onError(sink::error)
                    .start();
        });
    }
}
